package com.woniu.job.controller;

import com.woniu.job.entity.Resume;
import com.woniu.job.entity.User;
import com.woniu.job.service.IResumeService;
import com.woniu.job.util.Helper;
import org.apache.zookeeper.CreateMode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("resume")
public class ResumeController {

    @Resource
    private IResumeService iResumeService;

    @RequestMapping("add")
    public String resAdd(Resume resume, HttpSession session,Model model){
        String id = Helper.getId();
        User user = (User)session.getAttribute("user");
        String categoryId = "15";
        String skill = "篮球，rap";
        resume.setUserId("15641316");
        resume.setSkill(skill);
        resume.setId(id);
        resume.setCategoryId(categoryId);
        resume.setStatus("1");
        resume.setUserId(user.getId());
        boolean t = iResumeService.save (resume);
        resume = iResumeService.getById(id);
        LocalDate today = LocalDate.now();
        int age = (int)(ChronoUnit.YEARS.between(resume.getBirthdate(),today));
        model.addAttribute("resume", resume);
        model.addAttribute("age",age);
        return "personal/resume_view";
    }

    @GetMapping("view")
    public String resView(Model model,String id){
        Resume resume = iResumeService.getById(id);
        model.addAttribute("resume",resume);
        return "personal/resume_view";
    }

    @GetMapping("allResume")
    public String getByUserId(Model model, HttpSession session){
        User user = (User)session.getAttribute("user");
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("userId",user.getId());
        List<Resume> resumes = iResumeService.selectByMap(columnMap);

        model.addAttribute("resumes",resumes);
        return "personal/resume_manage";
    }

    @GetMapping("del/{id}")
    public String del(@PathVariable String id, HttpSession session, Model model){
        iResumeService.deleteById(id);
        User user = (User)session.getAttribute("user");
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("userId",user.getId());
        List<Resume> resumes = iResumeService.selectByMap(columnMap);

        model.addAttribute("resumes",resumes);
        return "personal/resume_manage";
    }
    @GetMapping("/update/{id}")
    public String update(@PathVariable String id, Model model){
        Resume resume = iResumeService.getById(id);
        LocalDate today = LocalDate.now();
        int age = (int)(ChronoUnit.YEARS.between(resume.getBirthdate(),today));
        model.addAttribute("resume",resume);
        model.addAttribute("age",age);
        return "personal/resume_ed";
    }

    @PostMapping("/update/{id}")
    public String update(Resume resume, @PathVariable String id){
        System.out.println("页面id传入:"+id);
        resume.setId(id);
        boolean t = iResumeService.updateById(resume);
        return "personal/resume_view";
    }
}
